//
//  GAOComposeTextFiled.swift
//  Weibo28
//
//  Created by 高辰 on 16/5/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
//自定义textView
class GAOComposeTextView: UITextView {
    
    //自定义placeholder
    private lazy var placeholderLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(12)
        label.numberOfLines = 0
        label.textColor = UIColor.grayColor()
        return label
    }()
    
    //设置占位文字
    var placeholder: String? {
        didSet {
            placeholderLabel.text = placeholder
        }
    }
    //令外界设置TextView的文字大小的同时改变placeholder文字大小
    override var font: UIFont? {
        didSet {
            placeholderLabel.font = font
        }
    }
    
    //重写copy方法（对复制表情进行处理）
    override func copy(sender: AnyObject?) {
        var copyString = ""
        
        attributedText.enumerateAttributesInRange(selectedRange, options: []) { (result, range, _) -> Void in
//            print(result["NSAttachment"])
            if let attachment = result["NSAttachment"] as? GAOComposeTextAttachment
            {
                copyString += attachment.emoticonModel!.chs!
//                print(copyString)
            }
            else
            {
                let str = (self.attributedText.string as NSString).substringWithRange(range)
                copyString += str
            }
        }
        let pasteboard = UIPasteboard.generalPasteboard()
        pasteboard.string = copyString
    }
    //重写cut方法（对剪切表情进行处理）
    override func cut(sender: AnyObject?) {
        var copyString = ""
        attributedText.enumerateAttributesInRange(selectedRange, options: NSAttributedStringEnumerationOptions(rawValue: 0)) { (result, range, _) -> Void in
            if let attachment = result["NSAttachment"] as? GAOComposeTextAttachment
            {
                copyString += attachment.emoticonModel!.chs!
               
            }
            else
            {
                let str = (self.attributedText.string as NSString).substringWithRange(range)
                copyString += str
                
            }
        }
        super.cut(sender)
        
        let pasteboard = UIPasteboard.generalPasteboard()
        pasteboard.string = copyString
    }

    
    
    
    var observer: NSObjectProtocol?
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
        
        //监听textView文字改变的通知
        observer = NSNotificationCenter.defaultCenter().addObserverForName(UITextViewTextDidChangeNotification, object: nil, queue: nil) { [weak self] (_) -> Void in
            
            if self == nil {
                return
            }
            
            self!.placeholderLabel.hidden = self!.hasText()
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(placeholderLabel)
        //设置约束
        placeholderLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: placeholderLabel, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1, constant: -10))
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //指定位置
        placeholderLabel.x = 5
        placeholderLabel.y = 8
        
    }
    
    deinit {
        
        NSNotificationCenter.defaultCenter().removeObserver(observer!)
        print("over")
    }
    
}